let timeago = {
  curTime: new Date().getTime(),
  curTimeItem: function () {
    return this.getDateItem(this.curTime)
  },

  set: function (timeago) {
    let min = 60 * 1000,
      half = 30 * min,
      hour = 2 * half;
    let timeagoItem = this.getDateItem(timeago);
    let str = '';

    if (this.isThisDay(timeago)) {
      let interval = this.curTime - timeago * 1000;
      if (interval < min) {
        str = '刚刚';
      } else if (interval < hour) {
        str = Math.floor(interval / min) + '分钟前';
        if (interval >= half && interval <= half + min) {
          str = '半小时前';
        }
      } else if (interval >= hour) {
        str = Math.floor(interval / hour) + '小时前';
      }
    } else if (this.isYesterday(timeago)) {
      str = '昨天' + timeagoItem.hour + ':' + timeagoItem.min;
    } else if (this.isBeforeYesterday(timeago)) {
      str = '前天' + timeagoItem.hour + ':' + timeagoItem.min;
    } else if (this.isThisYear(timeago)) {
      str = timeagoItem.month + '月' + timeagoItem.day + '日';
    } else {
      str = timeagoItem.year + '年' + timeagoItem.month + '月' + timeagoItem.day + '日';
    }

    return str;
  },
  getDateItem: function (timestamp) {
    let value = ('' + timestamp).length > 10 ? timestamp : timestamp * 1000;
    let date = new Date(value);
    let h = date.getHours(),
      m = date.getMinutes();

    return {
      year: date.getFullYear(),
      month: date.getMonth() + 1,
      day: date.getDate(),
      hour: h > 9 ? h : '0' + h,
      min: m > 9 ? m : '0' + m
    }
  },
  isThisDay: function (timeago) {
    let day = this.getDateItem(timeago);
    let curTimeItem = this.curTimeItem();
    return day.year === curTimeItem.year &&
      day.month === curTimeItem.month &&
      day.day === curTimeItem.day;
  },
  isThisMonth: function (timeago) {
    let day = this.getDateItem(timeago);
    let curTimeItem = this.curTimeItem();
    return day.year === curTimeItem.year && day.month === curTimeItem.month;
  },
  isThisYear: function (timeago) {
    let day = this.getDateItem(timeago);
    let curTimeItem = this.curTimeItem();
    return day.year === curTimeItem.year;
  },
  isYesterday: function (timeago) {
    let day = this.getDateItem(timeago);
    let curTimeItem = this.curTimeItem();
    return this.isThisMonth(timeago) && day.day === curTimeItem.day - 1;
  },
  isBeforeYesterday: function (timeago) {
    let day = this.getDateItem(timeago);
    let curTimeItem = this.curTimeItem();
    return this.isThisMonth(timeago) && day.day === curTimeItem.day - 2;
  }
};

function getExpectedTime(dateTimeStamp) {
  var result = '';
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  if (dateTimeStamp != 0) {
    var now = new Date().getTime();
    var diffValue = dateTimeStamp * 1000 - now;
    if (diffValue > 0) {
      var dayC = diffValue / day;
      var hourC = diffValue / hour;
      var minC = diffValue / minute;
      if (dayC >= 1) {
        result = parseInt(dayC) + "天后";
      } else if (hourC >= 1) {
        result = parseInt(hourC) + "个小时后";
      } else if (minC >= 1) {
        result = parseInt(minC) + "分钟后";
      } else {
        result = '马上'
      }
    }
  }

  return result;
}

function getExpectedTime2(dateTimeStamp) {
  var result = '';
  var minute = 1000 * 60;
  var hour = minute * 60;
  if (dateTimeStamp != 0) {
    var now = new Date().getTime();
    var diffValue = dateTimeStamp - now;
    if (diffValue > 0) {
      var hourC = parseInt(diffValue / hour);
      var minC = parseInt((diffValue - hourC * hour) / minute);
      if (hourC > 0) {
        result +=  hourC + '小时'
      }
      if (minC > 0) {
        result += minC + '分钟'
      }

      // if (dayC >= 1) {
      //   result = parseInt(dayC) + "天后";
      // } else if (hourC >= 1) {
      //   result = parseInt(hourC) + "个小时后";
      // } else if (minC >= 1) {
      //   result = parseInt(minC) + "分钟后";
      // } else {
      //   result = '马上'
      // }
    }
  }

  return result;
}

function getTimeGroup(dateTimeStamp) {
  var result = {hour:0,min:0,sec:0};
  var second = 1000;
  var minute = second * 60;
  var hour = minute * 60;
  if (dateTimeStamp != 0) {
    var now = new Date().getTime();
    var diffValue = dateTimeStamp - now;
    if (diffValue > 0) {
      var hourC = parseInt(diffValue / hour);
      var minC = parseInt((diffValue - hourC * hour) / minute);
      var secC = parseInt((diffValue - hourC * hour - minC * minute) / second);
      if (hourC > 0) {
        result.hour = hourC;
      }
      if (minC > 0) {
        result.min = minC;
      }
      if (secC > 0) {
        result.sec = secC;
      }
    }
  }

  return result;
}

/**
 * 音频时长
 */
function getAudioShowTime(time) {
  time = parseInt(time);
  if (time < 10) {
    time = '00:0' + time;
  } else if (time < 60) {
    time = '00:' + time;
  } else {
    var min = 60;
    var minute = parseInt(time / min);
    minute = minute < 10 ? '0' + minute : minute;
    var second = time - min * minute;
    second = second < 10 ? '0' + second : second;
    time = minute + ':' + second;
  }
  return time;
}

/**
 * 时间样式转化
 */
function formatTimestamp(timestamp, fmt) {
  if (timestamp == null || timestamp == '' || timestamp == '0') {
    return ''
  } else {
    var date = new Date(timestamp * 1000)

    var o = {
      "M+": date.getMonth() + 1,                 //月份   
      "d+": date.getDate(),                    //日   
      "h+": date.getHours(),                   //小时   
      "m+": date.getMinutes(),                 //分   
      "s+": date.getSeconds(),                 //秒   
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
      "S": date.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }
}

module.exports = {
  timestampFormat: timeago,
  getExpectedTime: getExpectedTime,
  getExpectedTime2: getExpectedTime2,
  getAudioShowTime: getAudioShowTime,
  formatTimestamp: formatTimestamp,
  getTimeGroup: getTimeGroup,
}